package com.yx.cloud.map.layer;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.view.MotionEvent;

import com.aaa.lib.map.MapUtils;
import com.aaa.lib.map.MapView;
import com.yx.cloud.map.YXMapView;
import com.iot.common.utils.LogUtils;

public class INXNIAreaDivideLineLayer extends YXAreaDivideLineLayer {
    public static final String TAG = "YXINXNIAreaDivideLineLayer";
    private LinearGradient linearGradient;
    private RadialGradient radialGradient;
    private int[] gradient = new int[]{0xFFFDC282, 0xFFFCF3DC, 0xFFFDE3AC, 0xFFFDC282};

    public INXNIAreaDivideLineLayer(MapView mapView) {
        super(mapView);
        linearGradient = new LinearGradient(0, 0, pointRadus , 0, new int[]{0xFFFCF3DC, 0xFFFDE3AC, 0xFFFDC282}, null, Shader.TileMode.MIRROR);
        radialGradient = new RadialGradient(p1.x, p1.y, pointRadus*2, gradient, null, Shader.TileMode.MIRROR);
    }

    @Override
    public void setLine(PointF start, PointF end) {
        super.setLine(start, end);
//        float distance = (float) MapUtils.distance(p1.x, p1.y, p2.x, p2.y);

    }

    protected boolean handleTouch(MotionEvent event) {
        LogUtils.i(" x : " + event.getX() + "y " + event.getY());
        //获取反转矩阵 ，用于获取坐标平移前的点
        MapUtils.getInverseRotatedPoint(mMapView.getTransform(), event.getX(), event.getY(), tmpPoint);
        float x = tmpPoint[0];
        float y = tmpPoint[1];
        LogUtils.i(" x : " + x + "y " + y);

        float scale = ((YXMapView)mMapView).getScale();

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //判断是否点击端点 准备拖动
            if (MapUtils.isInCircle(x, y, p1.x, p1.y, pointRadus * 3 / scale)) {
                isTouchP1 = true;
            } else if (MapUtils.isInCircle(x, y, p2.x, p2.y, pointRadus * 3 / scale)) {
                isTouchP2 = true;
            } else if (MapUtils.pointToLine(x, y, p1.x, p1.y, p2.x, p2.y) < pointRadus * 3 / scale) {
                isTouchLine = true;
                lastPosX = x;
                lastPosY = y;
            } else {
                return false;
            }
            setCrossPoints(null,null);
            return true;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            LogUtils.i("move to x: " + x + " y : " + y);
            //将端点移到拖动位置
            if (isTouchP1) {
                movePoint(p1, x, y);
            } else if (isTouchP2) {
                movePoint(p2, x, y);
            } else if (isTouchLine) {
                float tempX = x - lastPosX;
                float tempY = y - lastPosY;
                movePoint(p1, p1.x + tempX, p1.y + tempY);
                movePoint(p2, p2.x + tempX, p2.y + tempY);
                lastPosX = x;
                lastPosY = y;
            } else {
                return false;
            }
            mMapView.refresh();

            return true;

        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (isTouchP1 || isTouchP2 || isTouchLine) {
                float distance = (float) MapUtils.distance(p1.x, p1.y, p2.x, p2.y);
                radialGradient = new RadialGradient(p1.x, p1.y, distance, gradient, null, Shader.TileMode.MIRROR);
                isTouchP1 = false;
                isTouchP2 = false;
                isTouchLine =false;
                refreshDivider();
                return true;
            }
        }
        return false;
    }

    @Override
    public void draw(Canvas canvas) {
        float scale = ((YXMapView)mMapView).getScale();

        canvas.save();

        //清除虚线效果
        canvas.setMatrix(mMapView.getTransform());
        //设置虚线效果 画虚线
        mPaint.setShader(radialGradient);
        mPaint.setStrokeWidth(6 / scale);
        mPaint.setPathEffect(mDashPath);
        canvas.drawLine(p1.x, p1.y, p2.x, p2.y, mPaint);

        mPaint.setPathEffect(null);
        if (p3 != null && p4 != null) {
            canvas.drawLine(p3.x, p3.y, p4.x, p4.y, mPaint);
        }
        //画两个端点的圆
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setShader(null);
        mPaint.setColor(Color.LTGRAY);
        canvas.drawCircle(p1.x, p1.y, (pointRadus+1) / scale, mPaint);
        canvas.drawCircle(p2.x, p2.y, (pointRadus+1) / scale, mPaint);
        mPaint.setShader(linearGradient);
        canvas.drawCircle(p1.x, p1.y, pointRadus / scale, mPaint);
        canvas.drawCircle(p2.x, p2.y, pointRadus / scale, mPaint);
        canvas.restore();
    }

    @Override
    public void release() {

    }


}
